package com.qf.dao.impl;

import com.qf.dao.BookDao;
import com.qf.domain.Book;
import com.qf.util.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BookDaoImpl implements BookDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public List<Book> selectAll() {
        String sql="select id,title,author,price from book";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Book.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(int bookId) {
        String sql="delete from book where id=?";
        try {
            queryRunner.update(sql,bookId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Book selectById(int bookId) {
        String sql=" select * from book where id=? ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Book.class),bookId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(Book book) {
        String sql=" update book set title=?,author=?,price=? where id=? ";
        Object[] params={book.getTitle(),book.getAuthor(),book.getPrice(),book.getId()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insert(Book book) {
        String sql=" insert into book values (null,?,?,?) ";
        Object[] params={book.getTitle(),book.getAuthor(),book.getPrice()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long selectCount() {
        try {
            return queryRunner.query(" select count(*) from book ",new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Book> selectPage(int page_num, int page_size) {
        String sql=" select * from book order by id limit ?,? ";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Book.class),(page_num-1)*page_size,page_size);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long selectCount(String where, List<Object> params) {
        try {
            String sql=" select count(*) from book "+where;
            return queryRunner.query(sql,new ScalarHandler<>(),params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Book> selectPage(int page_num, int page_size, String where, List<Object> params) {
        String sql=" select * from book "+where+" order by id limit ?,? ";
        params.add((page_num-1)*page_size);
        params.add(page_size);
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Book.class),params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
